package com.seeTools.boot.pc.service.sys.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seeTools.boot.api.entity.sys.SysTenant;
import com.seeTools.boot.common.constant.Constants;
import com.seeTools.boot.common.core.PageQuery;
import com.seeTools.boot.pc.mapper.sys.SysTenantMapper;
import com.seeTools.boot.pc.service.sys.ISysTenantService;
import com.seeTools.boot.pc.util.PageQueryHelper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 租户信息表 服务实现类
 * </p>
 *
 * @author FallenDust
 * @since 2023-05-11
 */
@CacheConfig(cacheNames = "sysTenantCache")
@Service
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements ISysTenantService {

    @Resource
    private PageQueryHelper<SysTenant> pageQueryHelper;

    @Override
    public Page<SysTenant> pageInfo(PageQuery<SysTenant> pageQuery) {
        // 根据查询参数组装查询wrapper
        QueryWrapper<SysTenant> queryWrapper = getQueryWrapper(pageQuery.getDataObj());
        return dictionary(pageQueryHelper.pageInfo(this, pageQuery, queryWrapper));
    }

    @Override
    @Cacheable(cacheNames = Constants.CacheKey.TENANT, key = "#tenantId", unless = "#result == null")
    public SysTenant findByTenantIdCache(Long tenantId) {
        return this.getById(tenantId);
    }


    @Override
    @CacheEvict(cacheNames = Constants.CacheKey.TENANT, key = "#entity.id")
    public boolean updateById(SysTenant entity) {
        return super.updateById(entity);
    }

    @Override
    @CacheEvict(cacheNames = Constants.CacheKey.TENANT, key = "#entity.id")
    public boolean removeById(SysTenant entity) {
        return super.removeById(entity);
    }


    /**
     * 查询数据
     *
     * @param model
     * @return
     */
    @Override
    public List<SysTenant> selectDataList(SysTenant model) {
        QueryWrapper<SysTenant> queryWrapper = getQueryWrapper(model);
        return dictionary(list(queryWrapper));
    }

    /**
     * 获取查询条件
     *
     * @param model
     * @return
     */
    public QueryWrapper<SysTenant> getQueryWrapper(SysTenant model) {
        QueryWrapper<SysTenant> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysTenant> lambdaQueryWrapper = queryWrapper.lambda();
        if (ObjectUtil.isNotEmpty(model)) {
            // 租户名称
            if (CharSequenceUtil.isNotEmpty(model.getTenantName())) {
                lambdaQueryWrapper.like(SysTenant::getTenantName, model.getTenantName());
            }
        }
        return queryWrapper;
    }


    /**
     * 翻译数据
     *
     * @param page
     * @return
     */
    public Page<SysTenant> dictionary(Page<SysTenant> page) {
        if (ObjectUtil.isNotEmpty(page)) {
            List<SysTenant> list = page.getRecords();
            dictionary(list);
        }
        return page;
    }


    /**
     * 翻译
     *
     * @param list
     * @return
     */
    public List<SysTenant> dictionary(List<SysTenant> list) {
        if (CollUtil.isNotEmpty(list)) {
            list.forEach(item -> {
            });
        }
        return list;
    }
}
